package com.jinkeen.xiaxie.banner

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.get
import androidx.viewpager2.widget.ViewPager2
import com.jinkeen.xiaxie.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import java.util.*
import kotlin.collections.ArrayList
import kotlin.concurrent.fixedRateTimer

class BannerActivity : AppCompatActivity() {
    companion object {
        val describes = arrayOf("1", "2", "3", "4", "5")
    }

    private lateinit var viewPager2: ViewPager2
    private lateinit var tvDescribes: TextView
    private lateinit var lyDot: LinearLayout
    private lateinit var colors: ArrayList<Int>
    private lateinit var mTimer: Timer
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_banner)
        viewPager2 = findViewById(R.id.vp_banner)
        tvDescribes = findViewById(R.id.tv_describe)
        lyDot = findViewById(R.id.ll_dot)
        //初始化颜色
        initColors()
        //初始化文字下方的点
        initDot()

        //添加适配器
        val viewPagerAdapter = ViewPagerAdapter(colors)
        viewPager2.adapter = viewPagerAdapter
        //设置轮播图初始位置在50000，以保证可以手动前翻,实现伪无限轮播
        viewPager2.currentItem = 50000
        //注册轮播图的滚动事件监听器
        viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            //当页面滚动时触发的事件
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }

            //当页面被选中时触发的方法
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                //有多少个页面就写多少,这里有五种颜色就写5,除以5取余数
                val current = position % 5
                tvDescribes.text = describes[current]
                //当页面被选中的时候，改变指示点
                changeDots(current)
            }

            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
                //当页面空闲状态被改变的时候
                if (state == ViewPager2.SCROLL_STATE_IDLE) {
                    startTurns()
                } else {
                    mTimer.cancel()
                }
            }
        })
    }

    private fun initColors() {
        colors = ArrayList()
        colors.add(Color.BLUE)
        colors.add(Color.YELLOW)
        colors.add(Color.GREEN)
        colors.add(Color.RED)
        colors.add(Color.DKGRAY)
    }

    private fun initDot() {
        val layoutParams = LinearLayout.LayoutParams(10, 10)
        layoutParams.setMargins(4, 4, 4, 4)
        for (i in 0 until colors.size) {
            val view = View(this)
            view.setBackgroundResource(R.drawable.seletor_dot)
            view.layoutParams = layoutParams
            if (i == 0) {// 初始化后第一个点设置为红色
                view.isSelected = true
            }
            lyDot.addView(view)
        }
    }

    private fun changeDots(position: Int) {
        //先把所有的点都恢复为白色
        for (i in 0 until lyDot.childCount) {
            val view = lyDot.getChildAt(i)
            view.isSelected = false
        }
        //获取当前被选中的条目 设置为选中状态
        lyDot[position].isSelected = true
    }

    /**
     * 定时器轮播
     */
    private fun startTurns() {
        mTimer = fixedRateTimer("banner-timer", false, 3000, 3000) {
            var position = viewPager2.currentItem
            position++
            runOnUiThread {
                viewPager2.setCurrentItem(position, true)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        startTurns()
    }

    override fun onPause() {
        super.onPause()
        mTimer.cancel()
    }
}